<?php
ini_set('soap.wsdl_cache_enabled', '0'); 
ini_set('soap.wsdl_cache_ttl', '0');
//电力行业工业信息安全态势感知平台汇总信息数据接口
require_once './include/common.php';
require_once './include/DB.class.php';
require_once './include/OldSoapDiscovery.class.php';


class servicewsdl{
 
 	//录入资产
	public function assetadd($str)
	{
		
		try{
			
			$config = include_once './config.php';

			$xml=simplexml_load_string($str,'SimpleXMLElement',LIBXML_NOCDATA);
			if(empty($xml->data)){
				return show(0,'error',array('msg'=>'数据为空'));
				exit;
			}
			if(empty($xml->data->asset)){
				return show(0,'error',array('msg'=>'没有资产'));
				exit;
			}
			$sqlstr="insert into asset(`code`,`area`,`dip`,`dmac`,`dname`,`dmanufacturers`,`dtype`,`dmodel`,`businesstypes`,`system`,`agent`) values";
			$temp=array();
			foreach ($xml->data->asset as $v) {
				$temp[]=((array)$v);
			}
			foreach ($temp as $v) {
				$sqlstr.='("'.implode('","',$v['field']).'"),';
			}
			$sqlstr=substr($sqlstr, 0, -1);
			$sqlstr.=';';
			$db = DB::getInstance($config['host'],$config['user'],$config['password'],$config['database'],$config['character']);
			$flag = $db->execSql($sqlstr);
			if(!empty($flag)){
				return show(1,'success',array('msg'=>'录入成功'));
			}else{
				return show(0,'error',array('msg'=>'录入失败'));
			}

			}catch(\Exception $e){
				return show(0,'failed',array('msg'=>$e->getMessage()));
			}
	}
 	//录入事件
	public function eventadd($str)
	{
		
		try{
			
			$config = include_once './config.php';

			$xml=simplexml_load_string($str,'SimpleXMLElement',LIBXML_NOCDATA);
			if(empty($xml->data)){
				return show(0,'error',array('msg'=>'数据为空'));
				exit;
			}
			if(empty($xml->data->event)){
				return show(0,'error',array('msg'=>'没有事件'));
				exit;
			}
			$sqlstr="insert into event(`code`,`area`,`monitoringdevice`,`sourceip`,`purposeip`,`name`,`describe`,`etime`,`repeat`,`status`) values";
			$temp=array();
			foreach ($xml->data->event as $v) {
				$temp[]=((array)$v);
			}
			foreach ($temp as $v) {
				$sqlstr.='("'.implode('","',$v['field']).'"),';
			}
			$sqlstr=substr($sqlstr, 0, -1);
			$sqlstr.=';';
			$flag = DB::getInstance($config['host'],$config['user'],$config['password'],$config['database'],$config['character'])->execSql($sqlstr);
			if(!empty($flag)){
				return show(1,'success',array('msg'=>'录入成功'));
			}else{
				return show(0,'error',array('msg'=>'录入失败'));
			}


			}catch(\Exception $e){
				return show(0,'failed',array('msg'=>$e->getMessage()));
			}
	}
	//录入告警
	public function alarmadd($str)
	{
		
		try{
			
			$config = include_once './config.php';

			$xml=simplexml_load_string($str,'SimpleXMLElement',LIBXML_NOCDATA);
			if(empty($xml->data)){
				return show(0,'error',array('msg'=>'数据为空'));
				exit;
			}
			if(empty($xml->data->warn)){
				return show(0,'error',array('msg'=>'没有告警'));
				exit;
			}
			$sqlstr="insert into alarm(`code`,`area`,`monitoringdevice`,`sourceip`,`purposeip`,`name`,`describe`,`atime`,`repeat`,`status`) values";
			$temp=array();
			foreach ($xml->data->warn as $v) {
				$temp[]=((array)$v);
			}
			foreach ($temp as $v) {
				$sqlstr.='("'.implode('","',$v['field']).'"),';
			}
			$sqlstr=substr($sqlstr, 0, -1);
			$sqlstr.=';';
			$flag = DB::getInstance($config['host'],$config['user'],$config['password'],$config['database'],$config['character'])->execSql($sqlstr);
			if(!empty($flag)){
				return show(1,'success',array('msg'=>'录入成功'));
			}else{
				return show(0,'error',array('msg'=>'录入失败'));
			}

			}catch(\Exception $e){
				return show(0,'failed',array('msg'=>$e->getMessage()));
			}
	}
}
 





if(!file_exists('servicewsdl.wsdl')){
	$discovery = new SoapDiscovery("servicewsdl","servicewsdl");
	$filename = $discovery->getWSDL(); //生成XML文件
}

$ssa = new SoapServer("servicewsdl.wsdl");
$ssa->setClass("servicewsdl");  //指定一个类中的所有方法
$ssa->handle();

/*
如果开启了wsdl模式，请记住
1、每次修改了类文件，就需要删除wsdl文件，让他自动生成

*/